![](https://img-blog.csdnimg.cn/img_convert/40b8618919f52bbcdc841cdfaed6cf48.png)
共享参数的用法
SharedPreferences是Android的一个轻量级存储工具,采用的存储结构是Key-Value的键值对方式。
共享参数的存储介质是符合XML规范的配置文件。保存路径是:/data/data/应用包名/shared_prefs/文件名.xml
下面是一个共享参数的XML文件示例:
Mr Lee
![](https://img-blog.csdnimg.cn/img_convert/f697889a062f0dbb976dc1cdeb97716e.png)
![](https://img-blog.csdnimg.cn/img_convert/267c205e9317696588787cea2860ef98.png)
// 从share.xml中获取共享参数对象
SharedPreferences shared = getSharedPreferences("share", MODE_PRIVATE);
// 下面是写入共享参数的代码例子
SharedPreferences.Editor editor = shared.edit(); // 获得编辑器的对象
editor.putString("name", "Mr Lee"); // 添加一个名叫name的字符串参数
editor.putInt("age", 30); // 添加一个名叫age的整型参数
editor.putBoolean("married", true); // 添加一个名叫married的布尔型参数
editor.putFloat("weight", 100f); // 添加一个名叫weight的浮点数参数
editor.commit(); // 提交编辑器中的修改
// 下面是读取共享参数的代码例子
String name = shared.getString("name", ""); // 从共享参数中获得名叫name的字符串
int age = shared.getInt("age", 0); // 从共享参数中获得名叫age的整型数
boolean married = shared.getBoolean("married", false); // 从共享参数中获得名叫married的布尔数
float weight = shared.getFloat("weight", 0); // 从共享参数中获得名叫weight的浮点数
==========================================================================================
![](https://img-blog.csdnimg.cn/img_convert/96c8ddb867fb0157aaaa1ebc4eeab571.png)
![](https://img-blog.csdnimg.cn/img_convert/7dd3b23f9fe3a72297a89e13ff0726d0.png)
![](https://img-blog.csdnimg.cn/img_convert/124dac5bab5ed4309584d42d53b7fdc6.png)
![](https://img-blog.csdnimg.cn/img_convert/28d991ff6e0b93435007bd570f41f3a5.png)
package com.example.myapplication;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener
{
private SharedPreferences mShared; // 声明一个共享参数对象
private EditText et_name;
private EditText et_age;
private EditText et_height;
private EditText et_weight;
private boolean isMarried = false;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_name = findViewById(R.id.et_name);
et_age = findViewById(R.id.et_age);
et_height = findViewById(R.id.et_height);
et_weight = findViewById(R.id.et_weight);
CheckBox ck_married = findViewById(R.id.ck_married);
ck_married.setOnCheckedChangeListener(this);
findViewById(R.id.btn_save).setOnClickListener(this);
// 从share.xml中获取共享参数对象
mShared = getSharedPreferences("share", MODE_PRIVATE);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
isMarried = isChecked;
}
@Override
public void onClick(View v)
{
if (v.getId() == R.id.btn_save)
{
String name = et_name.getText().toString();
String age = et_age.getText().toString();
String height = et_height.getText().toString();
String weight = et_weight.getText().toString();
if (TextUtils.isEmpty(name))
{
ToastUtil.show(this, "请先填写姓名");
return;
} else if (TextUtils.isEmpty(age))
{
ToastUtil.show(this, "请先填写年龄");
return;
} else if (TextUtils.isEmpty(height))
{
ToastUtil.show(this, "请先填写身高");
return;
} else if (TextUtils.isEmpty(weight))
{
ToastUtil.show(this, "请先填写体重");
return;
}
SharedPreferences.Editor editor = mShared.edit(); // 获得编辑器的对象
editor.putString("name", name); // 添加一个名叫name的字符串参数
editor.putInt("age", Integer.parseInt(age)); // 添加一个名叫age的整型参数
editor.putLong("height", Long.parseLong(height)); // 添加一个名叫height的长整型参数
editor.putFloat("weight", Float.parseFloat(weight)); // 添加一个名叫weight的浮点数参数
editor.putBoolean("married", isMarried); // 添加一个名叫married的布尔型参数
editor.putString("update_time", DateUtil.getNowDateTime("yyyy-MM-dd HH:mm:ss"));
editor.commit(); // 提交编辑器中的修改
ToastUtil.show(this, "数据已写入共享参数");
}
}
}
![](https://img-blog.csdnimg.cn/img_convert/a0815892882f82cef6e92e3fd583a630.png)
![](https://img-blog.csdnimg.cn/img_convert/d4b2b45ed4815b7eb86212e9b19a9301.png)
![](https://img-blog.csdnimg.cn/img_convert/30b1ea4cc2d0d8d7e128c183a0f7c4d5.png)
package com.example.myapplication;
import android.content.Context;
import android.widget.Toast;
public class ToastUtil {
public static void show(Context ctx, String desc) {
Toast.makeText(ctx, desc, Toast.LENGTH_SHORT).show();
}
}
![](https://img-blog.csdnimg.cn/img_convert/3425d801cfa238f13a76f3596d330d7a.png)
![](https://img-blog.csdnimg.cn/img_convert/4ce389c76cb933fab91e5e8e6065f59b.png)
![](https://img-blog.csdnimg.cn/img_convert/bc67e2afb853fe82edebd63e66e70c93.png)
![](https://img-blog.csdnimg.cn/img_convert/f1c869afe6c02ebf0cf2ac3de4fdf01b.png)
=========================================================================================
之前保持的数据:
20220710152200
王12正国
![](https://img-blog.csdnimg.cn/img_convert/ecebb443da7e15b61da652bc14e506ce.png)
package com.example.myapplication;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.Map;
public class MainActivity extends AppCompatActivity
{
private TextView tv_share;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_share = findViewById(R.id.tv_share);
readSharedPreferences(); // 从共享参数中读取信息
}
// 从共享参数中读取信息
private void readSharedPreferences()
{
// 从share.xml中获取共享参数对象
SharedPreferences shared = getSharedPreferences("share", MODE_PRIVATE);
String desc = "共享参数中保存的信息如下:";
// 获取共享参数保存的所有映射配对信息
Map mapParam = (Map) shared.getAll();
// 遍历该映射对象,并将配对信息形成描述文字
for (Map.Entry item_map : mapParam.entrySet())
{
String key = item_map.getKey(); // 获取该配对的键信息
Object value = item_map.getValue(); // 获取该配对的值信息
if (value instanceof String) { // 如果配对值的类型为字符串
desc = String.format("%s\n %s的取值为%s", desc, key, shared.getString(key, ""));
} else if (value instanceof Integer) { // 如果配对值的类型为整型数
desc = String.format("%s\n %s的取值为%d", desc, key, shared.getInt(key, 0));
} else if (value instanceof Float) { // 如果配对值的类型为浮点数
desc = String.format("%s\n %s的取值为%f", desc, key, shared.getFloat(key, 0.0f));
} else if (value instanceof Boolean) { // 如果配对值的类型为布尔数
desc = String.format("%s\n %s的取值为%b", desc, key, shared.getBoolean(key, false));
} else if (value instanceof Long) { // 如果配对值的类型为长整型
desc = String.format("%s\n %s的取值为%d", desc, key, shared.getLong(key, 0L));
} else { // 如果配对值的类型为未知类型
desc = String.format("%s\n参数%s的取值为未知类型", desc, key);
}
}
if (mapParam.size() |